package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class Library {

    private List<Book> books = new ArrayList<Book>();
    private PrintStream printStream;
    private Scanner scanner;
    private List<Movie> movies = new ArrayList<Movie>();
    private List<User> users = new ArrayList<User>();
    private User user;

    public Library(PrintStream printStream, Scanner scanner, User user) {
        this.printStream = printStream;
        this.scanner = scanner;
        this.user = user;
        users.add(new User("Steven","stevenndaye@thoughtworks.com","0767897061","111-1111","password",false));
        users.add(new User("Chris","creade@thoughtworks.com","0767897062","222-2222","password", false));
    }

    public void welcomeMessage() {
        printStream.println("Welcome to our Library");
    }

    public void menuOptions() {
        String loginMessage;
        if(user.hasLoggedIn()){
            loginMessage = "5 => Logout";
            printStream.println("==============\nMenu Options : \n1 => View books\n2 => Reserve a book\n3 => Check membership\n4 => View movies\n"+loginMessage+"\n");
        }else {
            loginMessage = "5 => Login";
            printStream.println("==============\nMenu Options : \n1 => View books\n2 => Reserve a book\n3 => Check membership\n4 => View movies\n"+loginMessage+"\n");
        }
    }

    public void selectMenuOptions(int option) {

        HashMap<Integer, CustomerOptionHandler> hashMap = new HashMap<Integer, CustomerOptionHandler>();

        hashMap.put(option, new InvalidOption());
        hashMap.put(5, new UserLoginOption(this));
        hashMap.put(4, new MovieListingOption(this));
        hashMap.put(3, new CustomerMembershipOption(this));
        hashMap.put(2, new BookReservationOption(this)) ;
        hashMap.put(1, new BookListingOption(this));

        CustomerOptionHandler customerOptionHandler = hashMap.get(option);
        printStream.println(customerOptionHandler.customerOption());
    }

    public void addBook(Book book) {
        books.add(book);
    }

    public List<Book> getBooks(){
        return books;
    }

    public void validateCustomerChoice() {
        printStream.println("Enter your choice => ");
        String customerChoice = scanner.nextLine();
        for(char character : customerChoice.toCharArray()){
            if(Character.isLetter(character)){
                throw new NumberFormatException("Invalid input, the choices are numeric");
            }else {
                selectMenuOptions(Integer.parseInt(customerChoice));
            }
        }
    }
    public String bookReservationRequest(){
        printStream.println("Enter book name: ");
        return scanner.nextLine();
    }

    public void addMovie(Movie movie) {
        movies.add(movie);
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public String getUsers() {
        StringBuilder listUsers = new StringBuilder();
        for(int x = 0;x < users.size();x++){
            User user = users.get(x);
            listUsers.append(user.toString()+"\n");
        }
        return listUsers.toString();
    }

    public User getUser() {
        return user;
    }


    public String requestUsername() {
        printStream.println("Enter your username: ");
        return scanner.nextLine();
    }

    public String requestPassword() {
        printStream.println("Enter your password: ");
        return scanner.nextLine();
    }


}